home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / REFERENC / TPR / SOURCE.EXE / DEMOMOVE.PAS < prev    next >
Pascal/Delphi Source File  |  1992-08-07  |  3KB  |  106 lines

  1. { DEMOMOVE.PAS }
  2. program MoveDemo;
  3. { Demonstrates how Move procedure can be used to move large numbers
  4. of bytes around inside an array.  This demo stores lines of text
  5. into a large TextArray.  New lines are added by calling AddLine, which
  6. calls MakeAHole to slide the existing data in the TextArray to make
  7. room for new text.  AddLine then uses Move to copy the string data
  8. into TextArray.  Data is fetched using the procedure GetLine.
  9.   The variable CurSize keeps track of how many bytes of TextArray are
  10. currently in use.  CurLocation keeps track of the current position; if we
  11. were writing a text editor based on this approach, CurLocation would index
  12. into TextArray where the editor's cursor appears on the screen.  In other
  13. word, the next new line that we type would be inserted at CurLocation.
  14. }
  15.  
  16. const
  17.   MaxSize = 32000;
  18.   MARKER = 13;
  19.  
  20. var
  21.   TextArray : Array[0..MaxSize] of Char;
  22.   CurSize : Word;
  23.   CurLocation : Word;
  24.  
  25.  
  26. procedure MakeAHole ( Address : Word; Size : Word );
  27. { Slides data in the TextArray sideways, leaving a "hole" to fill
  28. with new data }
  29. begin
  30.   Move( TextArray[Address], TextArray[Address+Size], CurSize - Address + 1 );
  31. end;
  32.  
  33.  
  34.  
  35. procedure AddLine ( var Address : Word; S : String );
  36. { Adds the contents of S to the TextArray, starting at location Address }
  37. var
  38.   NumBytes : Integer;
  39. begin
  40.   NumBytes := Length(S) + 1;
  41.   S[0] := Chr(MARKER);
  42.   MakeAHole ( Address, NumBytes );
  43.   Move( S[0], TextArray[Address], NumBytes );
  44.   CurSize := CurSize + NumBytes;
  45.   Address := Address + NumBytes;
  46. end;
  47.  
  48.  
  49.  
  50. procedure GetLine ( Address : Word; var S : String );
  51. { On entry, Address points to a MARKER character in the text array.  GetLine
  52. copies the text between this marker and the next marker (or end of array)
  53. to S. }
  54. var
  55.   TempAddr : Word;
  56. begin
  57.   TempAddr := Address + 1;
  58.   while (TempAddr <= CurSize) and
  59.         (TextArray[TempAddr] <> Chr(MARKER)) do
  60.     Inc(TempAddr);
  61.   Move ( TextArray[Address+1], S[1], TempAddr - Address );
  62.   S[0] := chr( TempAddr - Address );
  63. end;
  64.  
  65.  
  66. procedure MoveAhead (var Address : Word );
  67. { Where address points to some location in TextArray, MoveAhead positions
  68. Address to the index location of the next Marker character. }
  69. begin
  70.   if  TextArray[Address] = chr(MARKER)  then
  71.     Inc(Address);
  72.   while (Address <= CurSize) and
  73.         (TextArray[Address] <> Chr(MARKER)) do
  74.     Inc(Address);
  75. end;
  76.  
  77.  
  78. var
  79.   I : Integer;
  80.   ALine : String;
  81.  
  82. begin
  83.   CurSize := 0;
  84.   CurLocation := 0;
  85.  
  86.   { Add some sample data into TextArray }
  87.   AddLine ( CurLocation, 'Line 1 I''m the first line of data');
  88.   AddLine ( CurLocation, 'Line 2 and I''m the second!');
  89.   AddLine ( CurLocation, 'Line 3 The quick brown fox jumped over the');
  90.   AddLine ( CurLocation, 'Line 4 lazy brown dog and fell and hurt herself.');
  91.  
  92.   CurLocation := 0;
  93.   MoveAhead ( CurLocation );
  94.   AddLine ( CurLocation, 'One more line!!!!!');
  95.  
  96.   CurLocation := 0;
  97.   for I := 1 to 5 do
  98.   begin
  99.     GetLine ( CurLocation, ALine );
  100.     Writeln( ALine );
  101.     MoveAhead ( CurLocation );
  102.   end;
  103.  
  104.   readln;
  105. end.
  106.